﻿#include "servermonitorcontroller.h"

#include <cpuuse.h>
#include <diskuse.h>
#include <memoryinfo.h>
#include <responesutils.h>
#include <serverinfo.h>
#include <servermonitorcenter.h>

ServerMonitorController::ServerMonitorController(QObject *parent)
    : HttpController(parent)
{
}

ServerMonitorController::~ServerMonitorController()
{
}

void ServerMonitorController::route(QString path, QString method, HttpRequest &request, HttpResponse &response)
{
    if (method == "GET")
    {
        if (path == "resourceinfo")
        {
            return getAllInfo(request, response);
        }
        if (path == "serverinfo")
        {
            return getServerInfo(request, response);
        }
        if (path == "cpuinfolist")
        {
            return getCpuInfoList(request, response);
        }
        if (path == "memoryinfolist")
        {
            return getMemoryInfoList(request, response);
        }
        if (path == "diskinfolist")
        {
            return getDiskInfoList(request, response);
        }
        if (path == "cpuuselist")
        {
            return getCupUseList(request, response);
        }
        if (path == "memoryuselist")
        {
            return getMemoryUseList(request, response);
        }
        if (path == "virtualmemoryuselist")
        {
            return getVirtualMemoryUseList(request, response);
        }
        if (path == "diskuselist")
        {
            return getDiskUseList(request, response);
        }
        if (path == "networkinfo")
        {
            return getNetworkInfoList(request, response);
        }
        if (path == "networkuse")
        {
            return getNetworkUseList(request, response);
        }
    }
    return ResponesUtils::erroResponse(response, QString("无效的API:%1").arg(path));
}

void ServerMonitorController::getAllInfo(HttpRequest &request, HttpResponse &response)
{
    QJsonObject jsonObject;

    ServerInfo serverInfo;
    ServerMonitorCenter::getInstance()->getServerInfo(serverInfo);
    jsonObject.insert("serverInfo", serverInfo.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));

    QJsonArray cpuInfoJsonArray;
    QList<CpuInfo> cpuInfoList;
    ServerMonitorCenter::getInstance()->getCpuInfoList(cpuInfoList);
    for (CpuInfo &info : cpuInfoList)
    {
        cpuInfoJsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("cpuInfoList", cpuInfoJsonArray);

    QJsonArray memoryInfoJsonArray;
    QList<MemoryInfo> memoryInfoList;
    ServerMonitorCenter::getInstance()->getMemoryInfoList(memoryInfoList);
    for (MemoryInfo &info : memoryInfoList)
    {
        memoryInfoJsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("memoryInfoList", memoryInfoJsonArray);

    QJsonArray diskInfoJsonArray;
    QList<DiskInfo> diskInfoList;
    ServerMonitorCenter::getInstance()->getDiskInfoList(diskInfoList);
    for (DiskInfo &info : diskInfoList)
    {
        diskInfoJsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("diskInfoList", diskInfoJsonArray);

    QJsonArray cpuUseJsonArray;
    QList<CpuUse> cpuUseList;
    ServerMonitorCenter::getInstance()->getCpuUseList(cpuUseList);
    for (CpuUse &use : cpuUseList)
    {
        cpuUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime", "selfUsageRate"}));
    }
    jsonObject.insert("cpuUseList", cpuUseJsonArray);

    QJsonArray memoryUseJsonArray;
    QList<MemoryUse> memoryUseList;
    ServerMonitorCenter::getInstance()->getMemoryUseList(memoryUseList);
    for (MemoryUse &use : memoryUseList)
    {
        memoryUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("memoryUseList", memoryUseJsonArray);

    QJsonArray virtualMemoryUseJsonArray;
    QList<MemoryUse> virtualMemoryUseList;
    ServerMonitorCenter::getInstance()->getVirtualMemoryUseList(virtualMemoryUseList);
    for (MemoryUse &use : virtualMemoryUseList)
    {
        virtualMemoryUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("virtualMemoryUseList", virtualMemoryUseJsonArray);

    QJsonArray diskUseJsonArray;
    QList<DiskUse> diskUseList;
    ServerMonitorCenter::getInstance()->getDiskUseList(diskUseList);
    for (DiskUse &use : diskUseList)
    {
        diskUseJsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("diskUseList", diskUseJsonArray);

    DiskUse diskUseAll = ServerMonitorCenter::getInstance()->getDiskUseAll();
    jsonObject.insert("diskUseAll", diskUseAll.toJsonObject(QBaseEntity::UnEmptyProperty));

    QJsonArray networkInfoJsonArray;
    QList<NetworkInfo> networkInfoList;
    ServerMonitorCenter::getInstance()->getNetworkInfoList(networkInfoList);
    for (NetworkInfo &item : networkInfoList)
    {
        networkInfoJsonArray.append(item.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("networkInfoList", networkInfoJsonArray);

    QJsonArray networkUseJsonArray;
    QList<NetworkUse> networkUseList;
    ServerMonitorCenter::getInstance()->getNetworkUseList(networkUseList);
    for (NetworkUse &item : networkUseList)
    {
        networkUseJsonArray.append(item.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    jsonObject.insert("networkUseList", networkUseJsonArray);

    return ResponesUtils::resultResponse(response, jsonObject);
}

void ServerMonitorController::getServerInfo(HttpRequest &request, HttpResponse &response)
{
    ServerInfo info;
    ServerMonitorCenter::getInstance()->getServerInfo(info);
    return ResponesUtils::resultResponse(response, info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
}

void ServerMonitorController::getCpuInfoList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<CpuInfo> list;
    ServerMonitorCenter::getInstance()->getCpuInfoList(list);
    for (CpuInfo &info : list)
    {
        jsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getMemoryInfoList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<MemoryInfo> list;
    ServerMonitorCenter::getInstance()->getMemoryInfoList(list);
    for (MemoryInfo &info : list)
    {
        jsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getDiskInfoList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<DiskInfo> list;
    ServerMonitorCenter::getInstance()->getDiskInfoList(list);
    for (DiskInfo &info : list)
    {
        jsonArray.append(info.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getCupUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<CpuUse> list;
    ServerMonitorCenter::getInstance()->getCpuUseList(list);
    for (CpuUse &use : list)
    {
        jsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime", "selfUsageRate"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getMemoryUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<MemoryUse> list;
    ServerMonitorCenter::getInstance()->getMemoryUseList(list);
    for (MemoryUse &use : list)
    {
        jsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getVirtualMemoryUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<MemoryUse> list;
    ServerMonitorCenter::getInstance()->getVirtualMemoryUseList(list);
    for (MemoryUse &use : list)
    {
        jsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getDiskUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<DiskUse> list;
    ServerMonitorCenter::getInstance()->getDiskUseList(list);
    for (DiskUse &use : list)
    {
        jsonArray.append(use.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getNetworkInfoList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<NetworkInfo> list;
    ServerMonitorCenter::getInstance()->getNetworkInfoList(list);
    for (NetworkInfo &item : list)
    {
        jsonArray.append(item.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}

void ServerMonitorController::getNetworkUseList(HttpRequest &request, HttpResponse &response)
{
    QJsonArray jsonArray;
    QList<NetworkUse> list;
    ServerMonitorCenter::getInstance()->getNetworkUseList(list);
    for (NetworkUse &item : list)
    {
        jsonArray.append(item.toJsonObject(QBaseEntity::IgnoreOutProperty, QStringList{"createTime", "updateTime"}));
    }
    return ResponesUtils::resultResponse(response, jsonArray);
}
